package cn.ruituo.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class JdbcDelete {
    public static void main(String[] args) {
        //修改科目表
        Scanner sc=new Scanner(System.in);
        try {
            Class.forName("com.mysql.jdbc.Driver");//Class
        } catch (ClassNotFoundException e) {
            System.out.println("没有找到驱动");
            e.printStackTrace();
            //System.exit(0);
            return;
        }
        String url="jdbc:mysql://localhost:3306/java72db?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8&rewriteBatchedStatements=true";
        Connection con=null;
        Statement st=null;
        System.out.println("请输入课程名：");
        String name=sc.nextLine();
        //方案1：替换特殊字符
        // ' "  %   @
        ///>>>name=name.replace("'","");
        String updateSql="delete from s_subject where subjectname='"+name+"'";
        System.out.println(updateSql);
        try {
            con= DriverManager.getConnection(url,"root","root1234");
            st=con.createStatement();
            int r=st.executeUpdate(updateSql);
            System.out.printf("成功delete了%d条",r);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
